# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=subunit
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}"  "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.3.0
pkgrel=3
pkgdesc="Python implementation of subunit test streaming protocol (mingw-w64)"
arch=('any')
url='https://launchpad.net/subunit'
license=('Apache')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python2-extras"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-testtools"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-extras"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-testtools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-fixtures"
              "${MINGW_PACKAGE_PREFIX}-python2-hypothesis"
              "${MINGW_PACKAGE_PREFIX}-python2-testscenarios"
              "${MINGW_PACKAGE_PREFIX}-python3-fixtures"
              "${MINGW_PACKAGE_PREFIX}-python3-hypothesis" 
              "${MINGW_PACKAGE_PREFIX}-python3-testscenarios")
options=('staticlibs' 'strip' '!debug')
source=("https://pypi.io/packages/source/p/python-subunit/python-subunit-$pkgver.tar.gz")
sha512sums=('387c9ce8ed679d8998d33c7a295412e4f9de46b8dccdfab92eb82d8c4290958760b6c9bd99d50abd0836e4556c1b0c0132fc01e16801a3113844ab1bf28cf962')

# Helper macros to help make tasks easier #

prepare() {
  cd "${srcdir}"
  sed -i 's|import unittest2 as unittest|import unittest|' python-subunit-$pkgver/python/subunit/tests/test_test_protocol.py
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "python-${_realname}-${pkgver}" "${builddir}"
  done
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
}

build() {
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
}

check() {
  for pver in {2,3}; do
    msg "Python ${pver} test for ${CARCH}"
    cd "${srcdir}/python${pver}-build-${CARCH}"
    PYTHONPATH="$PWD/build/lib:$PYTHONPATH" ${MINGW_PREFIX}/bin/python${pver} -m testtools.run subunit.test_suite || warning "Tests failed"
  done
}

package_python3-subunit() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-extras"
           "${MINGW_PACKAGE_PREFIX}-python3-testtools")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
  #fix so the correct interpretter is used
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for name in subunit-1to2 subunit-2to1 subunit-filter subunit-ls subunit-notify subunit-output subunit-stats subunit-tags subunit2csv subunit2gtk subunit2junitxml subunit2pyunit tap2subunit subunit2disk; do
    sed -e "s|${PREFIX_WIN}/bin/python3.exe|${MINGW_PREFIX}/bin/python3.exe|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/$name
  done
}

package_python2-subunit() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-extras"
           "${MINGW_PACKAGE_PREFIX}-python2-testtools")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for name in subunit-1to2 subunit-2to1 subunit-filter subunit-ls subunit-notify subunit-output subunit-stats subunit-tags subunit2csv subunit2gtk subunit2junitxml subunit2pyunit tap2subunit subunit2disk; do
#fix so the file is a script that can run with no issues on the command-line w/o
#specifying an interpretter
    sed -e "s|${PREFIX_WIN}/bin/python2.exe|${MINGW_PREFIX}/bin/python2.exe|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/$name
    mv "${pkgdir}${MINGW_PREFIX}"/bin/$name{,-python2}
  done

}

package_mingw-w64-i686-python2-subunit() {
  package_python2-subunit
}

package_mingw-w64-i686-python3-subunit() {
  package_python3-subunit
}

package_mingw-w64-x86_64-python2-subunit() {
  package_python2-subunit
}

package_mingw-w64-x86_64-python3-subunit() {
  package_python3-subunit
}
